package com.xhj.bigfactory.bigFactory.HashMap;

import java.util.LinkedList;
/**
 * -
 *
 * @author 见见 pure_xhj@163.com @Date 2021-08-08 @Time 22:37
 */
public class HashMapDemo {
  public static void main(String[] args) {
    MyHashMap hashMap = new MyHashMap();
    for (int i = 0; i < 10; i++) {
      hashMap.put(i + "", i + "");
    }
    System.out.println(hashMap.toString());
  }
}

class MyHashMap {
  int size = 10;
  Node[] array = new Node[size];
  LinkedList<Node>[] link = new LinkedList[size];

  public MyHashMap() {}

  public MyHashMap(int size) {
    this.size = size;
    this.array = new Node[size];
    this.link = new LinkedList[size];
  }

  public int put(Object key, Object value) {
    Node node = new Node(key, value);
    if (array[node.hashCode() % size] == null) {
      array[node.hashCode() % size] = node;
    } else {
      if (link[node.hashCode() % size] == null) {
        link[node.hashCode() % size] = new LinkedList<>();
        link[node.hashCode() % size].addLast(node);
      } else if (link[node.hashCode() % size].size() >= 8) {
        link[node.hashCode() % size].addLast(node);
        rePutAll(this);
      } else {
        link[node.hashCode() % size].addLast(node);
      }
    }
    return 1;
  }

  public MyHashMap rePutAll(MyHashMap hashMap) {
    MyHashMap map = new MyHashMap(hashMap.size * 2);
    for (int i = 0; i < hashMap.array.length; i++) {
      map.put(hashMap.array[i].key, hashMap.array[i].value);
      for (int j = 0; j < link[i].size(); j++) {
        LinkedList<Node> link = this.link[i];
        while (true) {
          Node node = link.pollFirst();
          if (node == null) break;
          map.put(node.key, node.value);
        }
      }
    }
    hashMap = map;
    return map;
  }

  public int remove(Object key) {
    return 0;
  }

  public Object get(Object key) {
    for (int i = 0; i < array.length; i++) {
      if (array[i].key.equals(key)) {
        return array[i].key;
      }
      for (int j = 0; j < link[i].size(); j++) {
        LinkedList<Node> link = this.link[i];
        while (true) {
          Node node = link.pollFirst();
          if (node == null) break;
          if (node.key.equals(key)) {
            return node.value;
          }
        }
      }
    }
    return null;
  }

  public String toString() {
    int s = 0;
    String str = "";
    for (int i = 0; i < array.length; i++) {
      if (array[i] != null) {
        str += ", " + array[i].key + ":" + array[i].value;
        if (s == 0) {
          str = str.substring(2);
        }
        s = 1;
      }
      if (link[i] == null) {
        continue;
      }
      for (int j = 0; j < link[i].size(); j++) {
        LinkedList<Node> link = this.link[i];
        while (true) {
          Node node = link.pollFirst();
          if (node == null) break;
          str += ", " + array[i].key + ":" + array[i].value;
        }
      }
    }
    return str;
  }

  class Node {
    Object key;
    Object value;

    public Node(Object key, Object value) {
      this.key = key;
      this.value = value;
    }
  }
}
